/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 11, 2013  6:13:29 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.interfaces.controller.fi.outbound.backlogs
 * File Name : DepositReturnBacklogsController.java
 * TODO
 */
package com.tis.interfaces.controller.fi.outbound.backlogs;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import promis.utility.CommonService;
import com.tis.database.entitys.promis.VwSyncDepositBl;
import com.tis.database.entitys.staging.ApplicationLoging;
import com.tis.database.services.promis.IVwSyncDepositBlServices;
import com.tis.interfaces.controller.commons.CommonController;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 11, 2013 : 6:13:29 PM 
 * CreateBy=Chatree Siangklom
 * TODO
 */
@Controller
@RequestMapping(value=DepositReturnBacklogsController.url)
public class DepositReturnBacklogsController extends CommonController {
	private static final Logger logging = LoggerFactory.getLogger(DepositReturnBacklogsController.class);
	public static final String  url = "/fi/outbound/rental/depositreturn/backlogs";
	public static final String  uri = "/fi/outbound/backlogs";
	
	@RequestMapping(value="index")
	public String BacklogsIndex(HttpServletRequest request, Model model) throws Exception {
		logging.info(url + "/backlogs/index {Index}");
		setCurrentAppsInstanceNo();
		KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
		try {
			//Put History 		
			try { UserAccessTime(request, this.toString()); }catch(Exception e) {e.printStackTrace();}
			
			if(!UserLoginValidation((request)))
				return "redirect:/login";
			
		} catch (Exception e) {
			logging.error(e.getMessage());
			throw e;
		}
		
		BacklogsForm form = new BacklogsForm();
		form.setVoucherType("");
		form.setMaxResult("5000");
		
		logging.info("form =" + form.toString());
		model.addAttribute("ObjectForm", form);
		return uri + "/depositreturn";
	}
	
	@RequestMapping(value="search", method = RequestMethod.POST)
	public String BacklogsSearch(HttpServletRequest request, 
								 Model model, 
								 @ModelAttribute("ObjectForm") 
								 @Valid	
								 BacklogsForm form, 
								 BindingResult result) throws Exception {
		try {
			logging.info(url + "/search {search}");
			setCurrentAppsInstanceNo();
			KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
			
			//Put History 		
			try { UserAccessTime(request, this.toString()); }catch(Exception e) {e.printStackTrace();}
			
			if(!UserLoginValidation((request)))
				throw new Exception("Can't Search, Please Login first !!!!");
			
		} catch (Exception e) {
			logging.error(e.getMessage());
			throw e;
		}
		
		return uri + "/depositreturn";
	}
	
	//@Autowired IVwSyncGlPostingDetailServices iVwSyncGlPostingDetailServices;
	
	@Autowired
	private IVwSyncDepositBlServices iVwSyncDepositBlServices;
	
	@RequestMapping(value="search/json", method = RequestMethod.GET, headers="accept=application/json")
	@ResponseBody
	public List<VwSyncDepositBl> SearchBacklogs(
														/*
														@RequestParam(value="company", required=false) String company,
														@RequestParam(value="project", required=false) String project,
														@RequestParam(value="documentNo", required=false) String documentNo,
														@RequestParam(value="voucherType", required=false) String voucherType,
														@RequestParam(value="syncIsstaging", required=false) String syncIsstaging,
														@RequestParam(value="syncIssync", required=false) String syncIssync,
														@RequestParam(value="status", required=false) String status,
														@RequestParam(value="refDocNo", required=false) String refDocNo,
														*/
														@RequestParam(value="maxResult", required=false) String maxResult,
														HttpServletRequest request, 
														HttpServletResponse response) throws Exception {
		
		logging.info(url + "search/json {Search} ::: method = RequestMethod.GET");
		setCurrentAppsInstanceNo();
		KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
		
		if(!UserLoginValidation((request))) {
			response.setStatus(500);
			return null;
		}
		
		try {
			
			if(!UserLoginValidation((request)))
				throw new Exception("Can't Search, Please Login first !!!!");
			
		} catch (Exception e) {
			logging.error(e.getMessage());
			throw e;
		}
		
		/*
		logging.info("company --> " +  company);
		logging.info("project --> " +  project);
		logging.info("documentNo --> " +  documentNo);
		logging.info("voucherType --> " +  voucherType);
		logging.info("syncIsstaging --> " +  syncIsstaging);
		logging.info("syncIssync --> " +  syncIssync);
		logging.info("status --> " +  status);
		logging.info("refDocNo --> " +  refDocNo);
		*/
		logging.info("maxResult --> " +  maxResult);
		
		int maxResultInt = -1;
		if(!CommonService.IsNull(maxResult))
			maxResultInt = (Integer.parseInt(CommonService.IsNullRZero(maxResult)) == 0) ? -1 : Integer.parseInt(CommonService.IsNullRZero(maxResult));
		
		//voucherType = "DP";
		//List<VwSyncGlPostingDetail> lst = iVwSyncGlPostingDetailServices.SearchByVoucherType(company, project, documentNo, voucherType, syncIsstaging, syncIssync, status, refDocNo, maxResultInt);
		List<VwSyncDepositBl> lst = iVwSyncDepositBlServices.SearchByIsNotSend("'A','Z', 'O'", null, "N", maxResultInt);
		logging.info("VwSyncDepositBl Size = " + CommonService.getSize(lst));
		
		/*
			for(VwSyncDepositBl value : lst)
				logging.info(value.toString());
		*/
		
		if(CommonService.getSize(lst) > 0) 
			return lst;
		
		return null;
	}
	
	
	
}
